package com.example.uidemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.uidemo.adapter.FruitAdapter;
import com.example.uidemo.adapter.FruitRecyclerAdapter;
import com.example.uidemo.dto.Fruit;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewDemo extends AppCompatActivity {

    private String[] data = {"apple", "banana", "orange", "watermelon", "pear", "Grape", "apple", "banana", "orange", "watermelon", "pear", "Grape", "apple", "banana", "orange", "watermelon", "pear", "Grape", "apple", "banana", "orange", "watermelon", "pear", "Grape", "apple", "banana", "orange", "watermelon", "pear", "Grape"};

    private List<Fruit> fruitList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view_demo);
        initFruits(); // 初始化
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_id);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        FruitRecyclerAdapter  fruitRecyclerAdapter= new FruitRecyclerAdapter(fruitList);
        recyclerView.setAdapter(fruitRecyclerAdapter);
    }

    public  void initFruits(){
        for (int i=0,j =data.length;i<j;i++){
            Fruit f = new Fruit(data[i],R.drawable.apple);
            fruitList.add(f);
            Fruit d = new Fruit(data[i],R.drawable.banana);
            fruitList.add(d);
        }
    }
}
